/**
 * 
 */
package com.pmsco.pms.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.pmsco.pms.ajaxdata.UserListJsonData;
import com.pmsco.pms.annotation.LogonRequired;
import com.pmsco.pms.annotation.PermissionRequired;
import com.pmsco.pms.business.PmsUserBusiness;
import com.pmsco.pms.business.dto.PmsUserRequestDTO;
import com.pmsco.pms.business.dto.PmsUserReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.core.domain.PmsUser;
import com.pmsco.pms.util.BundleUtil;
import com.pmsco.pms.util.PmsLogonRequired;
import com.pmsco.pms.util.PmsPermissionRequired;
import com.pmsco.pms.util.PmsSysInfo;

/**
 * @author Long Tran
 *
 */
@Controller
public class PmsUserController extends AbstractPmsController {
	private static final Logger logger = LoggerFactory.getLogger(PmsUserController.class);
	
	/**
	 * List PMS User
	 */
	@RequestMapping(value = PmsConstant.listUser_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_LIST_USER_FUNCTION)
	public String listUser(Locale locale, Model model) {
		logger.info("List User Action");
		return PmsConstant.listUser_View;
	}
	
	/**
	 * List PMS User Data
	 */
	@RequestMapping(value = PmsConstant.listUserData_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_LIST_USER_FUNCTION)
	public @ResponseBody UserListJsonData listUserData(HttpServletRequest request, Locale locale, Model model) {
		logger.info("Begin List User Data Action");
		PmsUserBusiness pmsUserBusiness = new PmsUserBusiness();
		
		logger.debug("Build list user request dto");
		PmsUserRequestDTO pmsUserRequestDTO = new PmsUserRequestDTO();
		Integer page = Integer.parseInt(request.getParameter("page"));
		Integer rows = Integer.parseInt(request.getParameter("rows"));
		String sidx = request.getParameter("sidx");
		String sord = request.getParameter("sord");
		String searchField = request.getParameter("searchField");
		String searchString = request.getParameter("searchString");
		String searchOper = request.getParameter("searchOper");
		if(page == null)
			page = 1;
		if(rows == null)
			rows = 10;
		pmsUserRequestDTO.setPage(page);
		pmsUserRequestDTO.setRows(rows);
		pmsUserRequestDTO.setSidx(sidx);
		pmsUserRequestDTO.setSord(sord);
		pmsUserRequestDTO.setSearchField(searchField);
		pmsUserRequestDTO.setSearchString(searchString);
		pmsUserRequestDTO.setSearchOper(searchOper);
		
		logger.debug("Call to pms user business for list user");
		PmsUserReturnDTO pmsUserReturnDTO = pmsUserBusiness.listUserData(pmsUserRequestDTO);
		
		logger.debug("Analysis list user return dto");
		UserListJsonData userListJsonData = new UserListJsonData();
		Integer records = pmsUserReturnDTO.getPmsUsersSize();
		Integer total = (records / rows);
		if(records % rows > 0)
			total += 1;
		userListJsonData.setPage(page);
		userListJsonData.setRecords(records);
		userListJsonData.setTotal(total);
		userListJsonData.setRows(pmsUserReturnDTO.getPmsUsers());
		
		logger.info("End List User Data Action");
		return userListJsonData;
	}
	
	/**
	 * Create PMS User
	 */
	@RequestMapping(value = PmsConstant.createUser_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_CREATE_USER_FUNCTION)
	public String createUser(Locale locale, Model model) {
		logger.info("Begin Create User Action");
		PmsUserBusiness pmsUserBusiness = new PmsUserBusiness();
		
		logger.debug("Call to pms user business for create user");
		PmsUserReturnDTO pmsUserReqturnDTO = pmsUserBusiness.createUser(new PmsUserRequestDTO());
		
		logger.debug("Analysis create user return dto");
		model.addAttribute("userDetail", pmsUserReqturnDTO.getPmsUser());
		model.addAttribute(PmsConstant.pmsRoleList, pmsUserReqturnDTO.getRoleList());
		
		logger.info("End Create User Action");
		return pmsUserReqturnDTO.getReturnView();
	}
	
	/**
	 * Edit PMS User
	 */
	@RequestMapping(value = PmsConstant.editUser_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_EDIT_USER_FUNCTION)
	public ModelAndView editUser(@RequestParam(value = "userName") String userName) {
		logger.info("Begin Edit User Action");
		PmsUserBusiness pmsUserBusiness = new PmsUserBusiness();
		
		logger.debug("Build edit user request dto");
		PmsUserRequestDTO pmsUserRequestDTO = new PmsUserRequestDTO();
		pmsUserRequestDTO.setUserName(userName);
		
		logger.debug("Call to pms user business for edit user");
		PmsUserReturnDTO pmsUserReqturnDTO = pmsUserBusiness.editUser(pmsUserRequestDTO);
		
		logger.debug("Analysis create user return dto");
		ModelAndView ret = new ModelAndView(pmsUserReqturnDTO.getReturnView(), "command", pmsUserReqturnDTO.getPmsUser());
		ret.addObject(PmsConstant.pmsRoleList, pmsUserReqturnDTO.getRoleList());
		
		logger.info("End Edit User Action");
		return ret;
	}
	
	/**
	 * Save PMS User
	 */
	@RequestMapping(value = PmsConstant.saveUser_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_SAVE_USER_FUNCTION)
	public ModelAndView saveUser(@ModelAttribute("userDetail") PmsUser userDetail, @RequestParam("roleId") Long roleId, Locale locale, Model model) {
		logger.info("Begin Save User Action");
		PmsUserBusiness pmsUserBusiness = new PmsUserBusiness();
		
		logger.debug("Build save user request dto");
		PmsUserRequestDTO pmsUserRequestDTO = new PmsUserRequestDTO();
		pmsUserRequestDTO.setPmsUser(userDetail);
		pmsUserRequestDTO.setRoleId(roleId);
		pmsUserRequestDTO.setEmailBody(BundleUtil.getInstance().getKey(PmsConstant.registerSuccessfulEmailBodyKey));
		pmsUserRequestDTO.setEmailSubject(BundleUtil.getInstance().getKey(PmsConstant.registerSuccessfulEmailSubjectKey));
		
		logger.debug("Call to pms user business for save user");
		PmsUserReturnDTO pmsUserReturnDTO = pmsUserBusiness.saveUser(pmsUserRequestDTO);
		
		logger.debug("Analysis save user return dto");
		ModelAndView ret = null;
		if(pmsUserReturnDTO.getReturnUrl() != null && !pmsUserReturnDTO.getReturnUrl().isEmpty()) {
			ret = new ModelAndView(new RedirectView(pmsUserReturnDTO.getReturnUrl()));
		} else {
			ret = new ModelAndView(pmsUserReturnDTO.getReturnView(), "command", pmsUserReturnDTO.getPmsUser());
			ret.addObject(PmsConstant.pmsRoleList, pmsUserReturnDTO.getRoleList());
			ret.addObject(PmsConstant.errorMessage, pmsUserReturnDTO.getErrorMessage());
		}
		
		logger.info("End Save User Action");
		return ret;
	}
	
	/**
	 * Register PMS User
	 */
	@RequestMapping(value = PmsConstant.registerUser_Url)
	@LogonRequired(required = PmsLogonRequired.NONE)
	public String registerUser(Locale locale, Model model) {
		logger.info("Register User Action");
		model.addAttribute("userDetail", new PmsUser());
		return PmsConstant.registerUser_View;
	}
	
	/**
	 * do Register PMS User
	 */
	@RequestMapping(value = PmsConstant.doRegisterUser_Url)
	@LogonRequired(required = PmsLogonRequired.NONE)
	public ModelAndView doRegisterUser(@ModelAttribute("userDetail") PmsUser userDetail, Locale locale, Model model) {
		logger.info("Begin Do Register User Action");
		PmsUserBusiness pmsUserBusiness = new PmsUserBusiness();
		
		logger.debug("Build register user request dto");
		PmsUserRequestDTO pmsUserRequestDTO = new PmsUserRequestDTO();
		pmsUserRequestDTO.setPmsUser(userDetail);
		pmsUserRequestDTO.setEmailBody(BundleUtil.getInstance().getKey(PmsConstant.registerSuccessfulEmailBodyKey));
		pmsUserRequestDTO.setEmailSubject(BundleUtil.getInstance().getKey(PmsConstant.registerSuccessfulEmailSubjectKey));
		
		logger.debug("Call to pms user business for register user");
		PmsUserReturnDTO pmsUserReqturnDTO = pmsUserBusiness.doRegisterUser(pmsUserRequestDTO);
		
		logger.debug("Analysis register user return dto");
		ModelAndView ret = null;
		if(pmsUserReqturnDTO.getReturnUrl() != null && !pmsUserReqturnDTO.getReturnUrl().isEmpty()) {
			ret = new ModelAndView(new RedirectView(pmsUserReqturnDTO.getReturnUrl()));
		} else {
			ret = new ModelAndView(pmsUserReqturnDTO.getReturnView(), "command", pmsUserReqturnDTO.getPmsUser());
			ret.addObject(PmsConstant.pmsRoleList, pmsUserReqturnDTO.getRoleList());
			ret.addObject(PmsConstant.errorMessage, pmsUserReqturnDTO.getErrorMessage());
		}
		
		logger.info("End Register User Action");
		return ret;
	}
	
	/**
	 * Register PMS User Successful
	 */
	@RequestMapping(value = PmsConstant.registerUserSuccessful_Url)
	@LogonRequired(required = PmsLogonRequired.NONE)
	public String registerUserSuccessful(Locale locale, Model model) {
		logger.info("Register User Successful Action");
		return PmsConstant.registerUserSuccessful_View;
	}
	
	/**
	 * Activate PMS User
	 */
	@RequestMapping(value = PmsConstant.activateUser_Url)
	@LogonRequired(required = PmsLogonRequired.NONE)
	public String activateUser(@RequestParam("ref") String userName, Locale locale, Model model) {
		logger.info("Begin Activate User Action");
		PmsUserBusiness pmsUserBusiness = new PmsUserBusiness();
		
		logger.debug("Build activate user request dto");
		PmsUserRequestDTO pmsUserRequestDTO = new PmsUserRequestDTO();
		pmsUserRequestDTO.setUserName(PmsSysInfo.getInstance().getPmsEncoder().decode(userName));
		
		logger.debug("Call to pms user business for activate user");
		PmsUserReturnDTO pmsUserReqturnDTO = pmsUserBusiness.activateUser(pmsUserRequestDTO);
		
		logger.info("End Activate User Action");
		return pmsUserReqturnDTO.getReturnView();
	}
	
	/**
	 * Fail Activate PMS User
	 */
	@RequestMapping(value = PmsConstant.failActivateUser_Url)
	@LogonRequired(required = PmsLogonRequired.NONE)
	public String failActivateUser(Locale locale, Model model) {
		logger.info("Fail Activate User Action");
		return PmsConstant.failActivateUser_View;
	}
	
	/**
	 * Unlock PMS User
	 */
	@RequestMapping(value = PmsConstant.unlockUser_Url)
	@LogonRequired(required = PmsLogonRequired.NONE)
	public String unlockUser(@RequestParam("ref") String userName, Locale locale, Model model) {
		logger.info("Begin Unlock User Action");
		PmsUserBusiness pmsUserBusiness = new PmsUserBusiness();
		
		logger.debug("Build unlock user request dto");
		PmsUserRequestDTO pmsUserRequestDTO = new PmsUserRequestDTO();
		pmsUserRequestDTO.setUserName(PmsSysInfo.getInstance().getPmsEncoder().decode(userName));
		
		logger.debug("Call to pms user business for unlock user");
		PmsUserReturnDTO pmsUserReqturnDTO = pmsUserBusiness.unlockUser(pmsUserRequestDTO);
		
		logger.info("End Unlock User Action");
		return pmsUserReqturnDTO.getReturnView();
	}

}
